package com.digibis.europeana4j.test;

import com.digibis.europeana4j.EuropeanaConnection;
import com.digibis.europeana4j.EuropeanaItem;
import com.digibis.europeana4j.EuropeanaQuery;
import com.digibis.europeana4j.EuropeanaResults;
import com.digibis.europeana4j.adv.EuropeanaComplexQuery;
import com.digibis.europeana4j.adv.EuropeanaFields;
import com.digibis.europeana4j.adv.EuropeanaOperand;
import com.digibis.europeana4j.adv.EuropeanaOperators;
import com.digibis.europeana4j.adv.EuropeanaSearchTerm;
import java.util.List;

/**
 *
 * @author Cosmin Coman
 */
public class Europeana4jComplexSearch {

    public static void main(String[] args) {
        try {
            long ms0 = System.currentTimeMillis();

            String apiKey = "";

            EuropeanaOperand opA = new EuropeanaOperand("Shakespeare");
            EuropeanaOperand opB = new EuropeanaOperand("William");
            EuropeanaOperand opC = new EuropeanaOperand("poetry");
            EuropeanaOperand opSimple = new EuropeanaOperand("dune");
            EuropeanaOperand opComplex = new EuropeanaOperand(EuropeanaOperators.AND, opA, opB);
            opComplex.addOperand(EuropeanaOperators.AND, opC);
            EuropeanaOperand opComplex2 = new EuropeanaOperand(EuropeanaOperators.OR, opComplex, opSimple);
            //System.out.println(opComplex2.toString());
            EuropeanaSearchTerm stSimple = new EuropeanaSearchTerm(EuropeanaFields.TITLE, opComplex2);
            //System.out.println(stSimple.toString());
            EuropeanaSearchTerm stComplex = new EuropeanaSearchTerm(EuropeanaFields.TITLE, opSimple);
            //System.out.println(stComplex.toString());
            stComplex.addSearchTerm(EuropeanaOperators.OR, stSimple);
            //System.out.println(stComplex.toString());
            
            //OR A QUICK AND SIMPLE SEARCH:
            //stComplex = new EuropeanaSearchTerm(EuropeanaFields.CREATOR, "eminescu");
            EuropeanaComplexQuery europeanaQuery = new EuropeanaComplexQuery(stComplex);
            //set query type
            europeanaQuery.setType(EuropeanaComplexQuery.TYPE.TEXT);
            EuropeanaConnection europeanaConnection = new EuropeanaConnection(apiKey);
            System.out.println("Query: " + europeanaQuery.getSearchTerms());
            System.out.println("Query url: " + europeanaQuery.getQueryUrl(europeanaConnection));
            EuropeanaResults res = europeanaConnection.search(europeanaQuery, 1000, 1);
            System.out.println("Results: " + res.getItemCount() + " / " + res.getTotalResults());
            if (res.getItemCount() > 0) {
                List<EuropeanaItem> items = res.getAllItems();
                for (int i = 0; i < items.size(); i++) {
                    EuropeanaItem item = items.get(i);
                    System.out.println();
                    System.out.println("**** " + (i + 1));
                    System.out.println("Title: " + item.getTitle());
                    System.out.println("Europeana URL: " + item.getObjectURL());
                    System.out.println("Type: " + item.getType());
                    System.out.println("Creator: " + item.getDcCreator());
                    System.out.println("Thumbnail: " + item.getBestThumbnail());
                    System.out.println("Data provider: "+ item.getDataProvider());
                    System.out.println("Id: "+ item.getId());
                    System.out.println("Guid: "+ item.getGuid());
                    System.out.println("Guid: "+ europeanaConnection.getRecordGuid(item.getId()));
                }
            }
            System.out.println();
            long t = System.currentTimeMillis() - ms0;
            System.out.println("*** OK!: " + (t / 1000d) + " seg.");
            //DISPLAYING A RECORD GIVEN A KNOWN GUID:
            //System.out.println(europeanaConnection.getSRWRecordAsString("http://www.europeana.eu/portal/record/9200169/3529381C3931F733CC59B704A0CAC7BBE8CCE280.html?utm_source=api&amp;utm_medium=api&amp;utm_campaign=QSRSVIYXYK%3c"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
